package com.huang825172.sbog.Tabs.Mine.RegisterPage.Store;

import android.content.Context;

import com.google.gson.Gson;
import com.huang825172.sbog.BaseFlux.Action.BaseAction;
import com.huang825172.sbog.BaseFlux.Store.BaseStore;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Mine.RegisterPage.Action.RegisterAction;
import com.huang825172.sbog.Tabs.Mine.RegisterPage.Store.Datas.Register;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class Store extends BaseStore {
    private String registerResponseRaw;
    private String exception;

    public Store(Context context) {
        super(context);
        exception = "";
    }

    @Override
    public List<String> getActionLabels() {
        List<String> labels = new ArrayList<>();
        labels.add("RegisterPageRegisterAction");
        labels.add("RegisterPageSendCheckAction");
        return labels;
    }

    @Override
    public void changeData(BaseAction action) {
        super.changeData(action);
        switch (action.getLabel()) {
            case "RegisterPageRegisterAction": {
                RegisterAction act = (RegisterAction) action;
                Register(act.getPhone(), act.getPassword());
                notifyDataChanged(action.getLabel());
                break;
            }
            case "RegisterPageSendCheckAction": {
                notifyDataChanged(action.getLabel());
                break;
            }
            default:
                break;
        }
    }

    private void Register(String phone, String password) {
        Register info = new Register(phone, password);
        Gson gson = new Gson();
        String infoStr = gson.toJson(info);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(context.getResources().getString(R.string.server_baseUrl))
                .build();
        Interfaces request = retrofit.create(Interfaces.class);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), infoStr);
        Call<ResponseBody> call = request.Register(body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    registerResponseRaw = response.body().string();
                } catch (Exception e) {
                    registerResponseRaw = null;
                    exception = e.getStackTrace().toString();
                    e.printStackTrace();
                }
                notifyDataChanged("RegisterPageRegisterAction");
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                registerResponseRaw = null;
                exception = t.getStackTrace().toString();
                t.printStackTrace();
                notifyDataChanged("RegisterPageRegisterAction");
            }
        });
    }

    public String getRegisterResponseRaw() {
        return registerResponseRaw;
    }

    public String getException() {
        return exception;
    }
}
